#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void reverse(char* a, int num)
{
    int i = 0;
    int j = num -1;
    while (i < j) {
        char tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
        ++i;
        --j;
    }
}

void multi(char* a, char* b)
{
    int m = strlen(a);
    int n = strlen(b);

    char* result = (char*)malloc(m+n+1);
    memset(result, '0', m+n);
    result[m+n] = 0;
    reverse(a, m);
    reverse(b, n);

    int i = 0;
    int j = 0;
    for (i = 0; i < n; ++i) {
        int addflag = 0;
        int multiflag = 0;
        for (j = 0; j < m; ++j) {
            int tmp1 = (a[j] - '0') * (b[i] - '0') + multiflag;
            multiflag = tmp1 / 10;
            tmp1 = tmp1 % 10;
            int tmp2 = (result[i+j] - '0') + tmp1 + addflag;
            addflag = tmp2 / 10;
            result[i+j] = tmp2 % 10 + '0';
        }
        result [i+m] +=  multiflag + addflag;
    }

    reverse(result, m+n);
    fprintf(stdout, "%s\n", "multi");
    if (result[0] != 48) 
        fprintf(stdout, "%c", result[0]);
    fprintf(stdout, "%s\n", result+1);
    free(result);
    result = NULL;

    reverse(a, m);
    reverse(b, n);
}

void add(char* a, char* b)
{
    int m = strlen(a);
    int n = strlen(b);

    int max = m;
    if (n > max) {
        max = n;
    }

    char * result = (char*) malloc (max+2);
    memset(result, '0', max+1);
    result[max+1] = 0;
    reverse(a, m);
    reverse(b, n);

    int i = 0;
    int flag = 0;
    for (i = 0; i < max + 1; ++i) {
        int tmpa = i < m ? a[i] - '0' : 0;
        int tmpb = i < n ? b[i] - '0' : 0;
        result[i] = (tmpa + tmpb + flag) % 10 + '0';
        flag = (tmpa + tmpb + flag) / 10;
    }
    reverse(result, max+1);
    fprintf(stdout, "%s\n", "add");
    if (result[0] != 48) {
        fprintf(stdout, "%c", result[0]);
    }
    fprintf(stdout, "%s\n", result+1);
    free(result);
    result = NULL;

    reverse(a, m);
    reverse(b, n);
}

int main () {
    char a[5] = {'9','8','7','9',0};
    char b[4] = {'7','8','9',0};
    fprintf(stdout, "%s\n", a);
    fprintf(stdout, "%s\n", b);
    multi(a, b);
    add(a, b);
    return 0;
}
